package com.xiaodou.mjf.application.core.manager;

import java.util.Date;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Component;

import com.xiaodou.mjf.application.basic.utils.SequenceGenerator;
import com.xiaodou.mjf.application.core.service.AccountService;
import com.xiaodou.mjf.application.core.service.DepositService;
import com.xiaodou.mjf.application.core.service.NotesBookService;
import com.xiaodou.mjf.entity.core.Account;
import com.xiaodou.mjf.entity.core.Deposit;
import com.xiaodou.mjf.entity.core.NotesBook;
import com.xiaodou.mjf.infrastructure.common.utils.DateUtils;

/**
 * Created by Rocky on 15/3/26.
 */
@Component
@Transactional
public class AccountManager {


    @Resource
    AccountService accountService;
    @Resource
    NotesBookService notesBookService;
    @Resource
    DepositService depositService;

    /***
     * 资金转入
     * @param account   转入账号
     * @param channelId 所属去到
     * @param amount    交易金额
     * @param type      交易类型
     * @param addiText  附加信息
     */
    public void transferIn(Account account, String channelId, Double amount, Integer type, String addiText){

        account = accountService.get(account.getId());

        Date currentTime = DateUtils.getCurrentTime();

        account.setPreviousBalance(account.getBookBalance());
        account.setAvaliableBalance(account.getAvaliableBalance() + amount);
        account.setBookBalance(account.getBookBalance() + amount);
        account.setLastDebitAmount(amount);
        account.setLastDebitTime(currentTime);

        NotesBook notesBook = new NotesBook();
        notesBook.setType(type);
        notesBook.setMemberId(account.getMemberId());
        notesBook.setAccount(account.getNumber());
        notesBook.setAdditionalText(addiText);
        notesBook.setBalance(account.getPreviousBalance());
        notesBook.setAmount(amount);
        //notesBook.setCardNumber();
        notesBook.setDescription(addiText);
        notesBook.setDrcr(NotesBook.DR);
        notesBook.setSerialNumber(SequenceGenerator.generateNotesBook(SequenceGenerator.TYPE_NOTEBOOK, channelId));
        notesBook.setTxnDate(currentTime);


        accountService.save(account);
        notesBookService.save(notesBook);


    }

    /**
     * 资金出
     * @param account   转出账号
     * @param channelId 所属渠道
     * @param amount    金额
     * @param type      类型
     * @param addiText  附加信息
     */
    public void transferOut(Account account, String channelId, Double amount, Integer type, String addiText){

        account = accountService.get(account.getId());

        Date currentTime = DateUtils.getCurrentTime();

        account.setPreviousBalance(account.getBookBalance());
        account.setAvaliableBalance(account.getAvaliableBalance() - amount);
        account.setBookBalance(account.getBookBalance() - amount);
        account.setLastCreditAmount(amount);
        account.setLastCreditTime(currentTime);

        NotesBook notesBook = new NotesBook();
        notesBook.setType(type);
        notesBook.setAccount(account.getNumber());
        notesBook.setAdditionalText(addiText);
        notesBook.setBalance(account.getPreviousBalance());
        notesBook.setAmount(amount);
        notesBook.setProcDate(currentTime);
        notesBook.setDescription(addiText);
        notesBook.setDrcr(NotesBook.CR);
        notesBook.setSerialNumber(SequenceGenerator.generateNotesBook(SequenceGenerator.TYPE_NOTEBOOK, channelId));
        notesBook.setTxnDate(currentTime);


        accountService.save(account);
        notesBookService.save(notesBook);

    }

    /**
     * 返现，发放代金券
     * @param account
     * @param channelId
     * @param amount
     * @param type
     * @param addiText
     */
    public void grantVoucher(Account account,String channelId,Double amount,Integer type,String addiText){

        account = accountService.get(account.getId());

        Date currentTime = DateUtils.getCurrentTime();

        account.setPreviousBalance(account.getBookBalance());
        account.setVoucherBalance(account.getVoucherBalance() + amount);
        account.setBookBalance(account.getBookBalance() + amount);
        account.setLastDebitAmount(amount);
        account.setLastDebitTime(currentTime);

        NotesBook notesBook = new NotesBook();
        notesBook.setType(type);
        notesBook.setMemberId(account.getMemberId());
        notesBook.setAccount(account.getNumber());
        notesBook.setAdditionalText(addiText);
        notesBook.setBalance(account.getPreviousBalance());
        notesBook.setAmount(amount);
        //notesBook.setCardNumber();
        notesBook.setDescription(addiText);
        notesBook.setDrcr(NotesBook.DR);
        notesBook.setSerialNumber(SequenceGenerator.generateNotesBook(SequenceGenerator.TYPE_NOTEBOOK, channelId));
        notesBook.setTxnDate(currentTime);
        notesBook.setProcDate(currentTime);


        accountService.save(account);
        notesBookService.save(notesBook);


    }

    /***
     * 充值
     * @param account
     * @param channelId
     * @param amount
     * @param type
     * @param addiText
     * @param tranChannelId
     * @param tranOrderId
     */
    public void deposit(Account account,String channelId,Double amount,Integer type,String addiText,String tranChannelId,String tranOrderId){

        Deposit deposit = new Deposit();
        deposit.setAmount(amount);
        deposit.setAccountId(account.getId());
        deposit.setChannelId(channelId);
        deposit.setFee(0D);
        deposit.setPurpose(addiText);
        deposit.setTranChannelId("");
        deposit.setTranChannelOrder("");
        deposit.setChannelSerial(SequenceGenerator.generateOrder(SequenceGenerator.TYPE_DEPOSIT));
        deposit.setTranChannelOrder(tranOrderId);
        deposit.setProcDate(DateUtils.getCurrentTime());
        deposit.setTranChannelId(tranChannelId);

        depositService.save(deposit);

        transferIn(account, channelId, amount, type, addiText);
    }

}
